package com.qiange.manager.controller;

import com.qiange.manager.Student.Student;
import com.qiange.manager.service.IStudentService;
import com.qiange.manager.service.impl.StudentServiceImpl;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentControllerImpl {
    private IStudentService studentService = new StudentServiceImpl();
    Scanner sc = new Scanner(System.in);

    public void start(){
        boolean flag = true;
        while (flag){
            System.out.println("欢迎访问学生管理系统");
            System.out.println("1. 添加");
            System.out.println("2. 查看");
            System.out.println("3. 删除");
            System.out.println("4. 修改");
            System.out.println("5. 退出");

            System.out.println("请输入您的选择");
            String choice = sc.next();

            switch (choice){
                case "1":
                    add();
                    break;
                case "2":
                    findAll();
                    break;
                case "3":
                    remove();
                    break;
                case "4":
                    set();
                    break;
                case "5":
                    System.out.println("感谢使用");
                    flag = false;
                    break;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }

    }

    private void set() {
        String sid = getIndex();
        int index = studentService.getIndexById(sid);
        Student student = getStudent(sid);
        boolean set = studentService.set(index, student);
        if (set){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }

    private Student getStudent(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();

        return new Student(sid, name, age);
    }

    private void remove() {
        String sid = getIndex();
        int index = studentService.getIndexById(sid);
        boolean remove = studentService.remove(index);
        if (remove){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    private String getIndex() {
        String sid;
        int index;
        while (true){
            System.out.println("请输入学号");
            sid = sc.next();
           index = studentService.getIndexById(sid);
            if (index == -1){
                System.out.println("学号不存在，请重新输入");
            }else {
                break;
            }
        }
        return sid;
    }

    private void findAll() {
        ArrayList<Student> list = studentService.findAll();
        if (list.size()==0){
            System.out.println("暂无数据，请重新输入");
            return;
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }

    private void add() {
        String sid;
        while (true){
            System.out.println("请输入学号");
             sid = sc.next();
            int index = studentService.getIndexById(sid);
            if (index != -1){
                System.out.println("学号已存在，请重新输入");
            }else {
                break;
            }
        }
        Student student = getStudent(sid);

        boolean add = studentService.add(student);
        if (add){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
}
